package Test1;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;


public class Test3{
    JFrame jf;
    JLabel resourceL, statusL;
    JTextField fileT;
    JButton analyseB;
    JPanel jp1, jp2, jp3, mainP;
    String fName;
    deadlock_detection dd;

    public Test3() {
        dd = new deadlock_detection();
        jf = new JFrame();
        resourceL = new JLabel();
        statusL = new JLabel();

        fileT = new JTextField(20);
        analyseB = new JButton("Analyse");
        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();
        mainP = new JPanel();

        jp1.add(fileT);
        jp1.add(analyseB);

        jp2.add(resourceL);

        jp3.add(statusL);

        mainP.add(jp1);
        mainP.add(jp2);
        mainP.add(jp3);

        mainP.setLayout(new GridLayout(3, 1));

        jf.add(mainP);
        jf.setVisible(true);
        jf.setTitle("Deadlock Case Analyser");
        jf.setSize(600, 200);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        analyseB.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                fName=fileT.getText();

                dd.readFile(fName);

                dd.deadlockDetection(fName);
                resourceL.setText(dd.resources);
                statusL.setText(dd.status);
            }
        });
    }

    public static void main(String args[]) {
        new Test3();
    }
}



class deadlock_detection{
    int P;
    int R;
    int available[];
    int allocation[][];
    int request[][];
    String resources;
    String status;
    int safeSequence[] = new int[P+3];


    public void readFile(String fileName) {

        try {
            Scanner sc = new Scanner(new File(fileName));
            String data = sc.nextLine();
            String datas[] = data.split(" ");
            P = Integer.parseInt(datas[datas.length-1]);
            data = sc.nextLine();
            datas = data.split(" ");
            R = Integer.parseInt(datas[datas.length-1]);


            available = new int[R];
            allocation = new int[P][R];
            request = new int[P][R];

            data = sc.nextLine();
            datas = data.split(" ");


            // Loops till number of resources
            int k = R;
            for(int i = 0; i < R; i++) {
                available[i] = Integer.parseInt(datas[datas.length-k]);
                k--;
            }
            data = sc.nextLine();



            // Loops till number of processes
            for(int i = 0; i < P; i++)

                for(int j = 0; j < R; j++)
                    allocation[i][j] = sc.nextInt();

            data = sc.nextLine();
            data = sc.nextLine();


            for(int i = 0; i < P; i++) {
                // Loops till number of resources
                for(int j = 0; j < R; j++) {

                    request[i][j] = sc.nextInt();
                }
            }
            sc.close();
        }


        catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("Unable to open the file: " + fileName);
        }
    }

    //definition of the method deadlockDetection
    public void deadlockDetection(String fileName) {
        resources = "Read case from: "+ fileName;

        int count=0;


        boolean visited[] = new boolean[P];
        for (int i = 0;i < P; i++) {
            visited[i] = false;
        }


        int work[] = new int[R];
        for (int i = 0;i < R; i++) {
            work[i] = available[i];
        }

        while (count<P) {
            boolean flag = false;
            for (int i = 0;i < P; i++) {
                if (visited[i] == false) {
                    int j;
                    for (j = 0;j < R; j++) {
                        if (request[i][j] > work[j])
                            break;
                    }
                    if (j == R) {
                        safeSequence[count++] = i;
                        visited[i]=true;
                        flag=true;

                        for (j = 0;j < R; j++) {
                            work[j] = work[j]+allocation[i][j];
                        }
                    }
                }
            }
            if (flag == false) {
                break;
            }
        }
        if (count < P) {
            status = "DEADLOCK";
        }
        else {
            status = "SAFE STATE";

        }
        status = "\nCase status: "+status;

    }
}



